<?php
class Suppliers {
    #constructor
    public function __construct($dbConnection)
    {
        $this->Connection=$dbConnection;
        $this->ID = NULL;
        $this->name = NULL;
        $this->contactService = NULL;
        $this->contactSalesOffice = NULL;
        $this->note = NULL;
        $this->inserted = false;
    }
    #end constructor

    private $Connection;
    
    public $ID;
    public $name;
    public $contactService;
    public $contactSalesOffice;
    public $note;
    public $inserted;


    #methods
    public function getAllSuppliers()
    {
        $queries = new mysqlqueries();
        $resultSuppliers = $this->Connection->query($queries->GetTable($queries->Supplier, "name"));
        $arr = array();
        while ($row = mysqli_fetch_array($resultSuppliers)){
            array_push($arr, $row);
        }
        return $arr;
    }
    public function getSupplierBy($index, $column)
    {
        if($column == null)
        {
            $column = "ID";
        }
        $queries = new mysqlqueries();
        $resultSupplier = $this->Connection->query($queries->GetIndex($queries->Supplier, $index, $column, null));
        if($rowSupplier = mysqli_fetch_array($resultSupplier))
        {
            return $rowSupplier; 
        }
        return false;
    }
    
    public function UpdateSupplier($id, $column,$argument) 
    {
      $queries = new mysqlqueries();
      $this->Connection->query($queries->Update($queries->Supplier, $argument, $id, $column, null));
      return true;
    }
    
    public function ExistsOrCreate(){
      $this->inserted = false;
        $queries = new mysqlqueries();
        $return = $this->getSupplierBy($this->name, "name");
        if(!$return){
            $upArray = array(NULL,$this->contactService, $this->contactSalesOffice, $this->note , $this->name);
            $this->Connection->query($queries->Add($queries->Supplier,$upArray));
            $this->ID = $this->Connection->lastID();
            $this->inserted = true;
        }
        else
        {
          $this->ID = $return["ID"];
        }
        return $this->ID;
    }
    
    public function DeleteSupplier($id)
    {
      $queries = new mysqlqueries();
      $this->Connection->query($queries->Remove($queries->Supplier, null, $id));
    }

    #end methods 
}
